<?php
if (!defined('BASEPATH'))
	exit ('No direct script access allowed');
/*
 * Created on Jul 7, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

/*
 *
 *
 */
function public_base_url() {
	return base_url() . 'public/';
}
/**
 *
 *
 */
function image_url() {
	return public_base_url() . 'admin/images/';
}
/**
 *
 *
 */
function userfile_base_url() {
	return base_url() . 'userfiles/';
}
/**
 *
 *
 */
function userfile_img_url($var) {
	$var = str_replace('userfiles/image/','',$var);
	return base_url() . 'userfiles/image/' . $var;
}
/**
 *
 *
 */
function js_tag($filepath){
	$result = sprintf("<script type='text/javascript' src='%s'></script>", $filepath);
	return $result;
}
/**
 * Show var dump. pre_var_dump() is used for testing only!
 *
 * @param mixed $var
 * @return null
 */
function pre_var_dump($var) {
	echo "<pre style=\"text-align: left\">\n";
	var_dump($var);
	echo "</pre>\n";
} // pre_var_dump

/** end general**/

/**
 * Return variable from an array
 *
 * If field $name does not exists in array this function will return $default
 *
 * @param array $from Hash
 * @param string $name
 * @param mixed $default
 * @param boolean $and_unset
 * @return mixed
 */
function array_var(& $from, $name, $default = null, $and_unset = false) {
	if (is_array($from) || (is_object($from) && instance_of($from, 'ArrayAccess'))) {
		if ($and_unset) {
			if (array_key_exists($name, $from)) {
				$result = $from[$name];
				unset ($from[$name]);
				return $result;
			} // if
		} else {
			return array_key_exists($name, $from) ? $from[$name] : $default;
		} // if
	} // if
	return $default;
} // array_var

/**
 *
 *
 */
function build_breakcum($sid, $breakCumb) {
	$result = $breakCumb;
	if (intval($sid) > 1) {
		$CI = & get_instance();
		$CI->load->model('Model_nav', 'model_nav');

		$parNode = $CI->model_nav->find($sid);
		if (!empty ($parNode)) {
			$result[] = $parNode;
			return build_breakcum($parNode['sid'], $result);
		}
	}
	$result = array_reverse($result);
	return $result;
}

function cms_send_mail($to, $subject, $message, $attach_files = null, $from = null, $name = null) {
	$CI = & get_instance();
	$config = $CI->config->item('email');
	//pre_var_dump($config);
	$CI->load->library('email');
	$CI->email->initialize($config);

	$CI->email->to($to);
	if ($from != null) {
		$CI->email->from($from, $name);
		//$CI->email->from('trangdt@purpleasia.com', 'dang thu trnag');
	}
	$CI->email->subject($subject);
	$CI->email->message($message);
	if (is_foreachable($attach_files)) {
		foreach ($attach_files as $file) {
			$CI->email->attach($file);
		}
	}
	$result = $CI->email->send();
	echo "<!--" . $CI->email->print_debugger() . "-->";
	return $result;
}

function select_access_level($name, $value, $options=""){
  $access_levels = array(
	  PUBLIC_ACCESS=>'Everyone',
	  MEMBER_ACCESS=>'Member',
	  DEALER_ACCESS=>'Dealer',
  );
  $html="<select name=\"{$name}\" {$options}>";
  $html.='<option value="">-----None-----</option>';
  foreach ($access_levels as $val=>$access_level){
    $checked =  $val== $value ? 'selected':'';
    $html.= "<option value=\"{$val}\" {$checked}>{$access_level}</option>";
  }
  $html.=  "</select>";
  echo $html;
}

/**
 *
 *
 */
function parse_input($prefix, $data){
	$CI =& get_instance();
	$result = array();
	if(is_array($result)){
		foreach ($data as $key => $val) {
			$pos = strpos($key, $prefix);
			if ($pos === false) {
			}
			else{
				$result[substr($key, strlen($prefix))] = $CI->input->post($key);
			}
		}
	}
	return $result;
}

/**
 * @param $str
 * @param $needle
 * @param $count
 * @return unknown_type
 */
function trimWord($str, $count, $needle = " ", $end = '')
{
	//$count = strpos($str, $count);
	$newstr = $str;
	if(strlen($newstr) > $count)
	{
		// Dem so ky tu unicode de cong them vao count
		$newstr = substr($newstr, 0, $count);
		// Tim kiem ky tu $needle o vi tri cuoi cung
		if(strrpos($newstr, $needle) !== false)
		{
			$newstr = substr($newstr, 0, strrpos($newstr, $needle)) ;
		}
		if(!empty($end)){
			$newstr .= $end;
		}
	}
	return $newstr;
}
/**
 *
 *
 */
function splitUppercase($str, $pattern = "/(.)([A-Z])/"){
	if(!empty($str)){
		$replacement = "\\1 \\2";
		return ucfirst(preg_replace($pattern, $replacement, $str));
	}
	return $str;
}
/**
 *
 *
 */
function select_option($list = array(), $name='', $selected='', $class=''){
	$str = "<select name='$name' class='$class'>";
	foreach($list as $key=>$value){
		$str .="<option value=$key selected=".($key==$selected)?'selected':''.">".$value."</option>";
	}
	$str .= "</select>";
	return $str;
}
/**
 *
 *
 */
function isXmlHttpRequest(){
	/* AJAX check  */
	if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
	    /* special ajax here */
	    return true;
	}
	return false;
}

function remove_accent($str)
{
  $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å',  'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å',  'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ', 'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę', 'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī', 'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı',  'Ĳ',  'ĳ', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ', 'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ŉ', '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'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o', 'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S', 's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W', 'w', 'Y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i', 'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
  // For Vietnamese
  $a = array_merge($a, array('Ố','Ồ','Ộ','Ổ','Ỗ','Ớ','Ờ','Ợ','Ở','ớ','ờ','ợ','ở', 'Ả','Ã','Â','Ấ','Ầ','Ậ','Ẩ','Ẫ','Ă','Ắ','Ằ','Ặ','Ẳ','ạ','ả','ã','ấ','ầ','ậ','ẩ','ẫ','ă','ắ','ằ','ặ','ẳ'));
  $b = array_merge($b, array('O','O','0','O','O','O','O','0','O','o','o','o','o', 'A','A','A','A','A','A','A','A','A','A','A','A','A','a','a','a','a','a','a','a','a','a','a','a','a','a'));

  $a = array_merge($a, array('É','È','Ẹ','Ẻ','Ẽ','Ê','Ế','Ề','Ệ','Ể','Ễ','é','è','ẹ','ẻ','ẽ','ê','ế','ề','ệ','ể','ễ','Ú','Ù','Ụ','Ủ','Ũ','Ư','Ứ','Ừ','Ự','Ử','Ữ','ú','ù','ụ','ủ','ũ','ư','ứ','ừ','ự','ử','ữ'));
  $b = array_merge($b, array('E','E','E','E','E','E','E','E','E','E','E','e','e','e','e','e','e','e','e','e','e','e','U','U','U','U','U','U','U','U','U','U','U','u','u','u','u','u','u','u','u','u','u','u'));

  $a = array_merge($a, array('Í','Ì','Ị','Ỉ','Ĩ','í','ì','ị','ỉ','ĩ','đ','Đ','Ý','Ỳ','Ỵ','Ỷ','Ỹ','ý','ỳ','ỵ','ỷ','ỹ'));
  $b = array_merge($b, array('I','I','I','I','I','i','i','i','i','i','d','D','Y','Y','Y','Y','Y','y','y','y','y','y'));
  return str_replace($a, $b, $str);
}

function post_slug($str)
{
  return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'),
  array('', '-', ''), remove_accent($str)));
}

?>
